package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.*;
import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

import com.oreilly.servlet.MultipartRequest;
import common.SaveUploadFile;
/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class EntInsertCommand extends Command {

	private String next;
	
	public EntInsertCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap listMap = new HashMap();	
	
		Connection con = null;  
		try {
			
			String uploadpath=QueryManager.getQuery("upload_datas.path");
			MultipartRequest multi=SaveUploadFile.getMultipartRequest(req,uploadpath);
			
			List list_multi = SaveUploadFile.savefile(multi,uploadpath);
			HashMap file_info =null;
			String file_name="";
			String file_id="";
			String file_size="0";
			String originFileName = "";
			String fileInput = "";
			
			String file_name_1="";
			String file_id_1="";
			String file_size_1="0";
			String originFileName_1 = "";
			
			String file_name_2="";
			String file_id_2="";
			String file_size_2="0";
			String originFileName_2 = "";
			
			String file_name_3="";
			String file_id_3="";
			String file_size_3="0";
			String originFileName_3 = "";
			
			for ( int i=0;list_multi!=null && list_multi.size()>i;i++){
				file_info = (HashMap)list_multi.get(i);
				file_name=(String)file_info.get("fileName");
				file_id=(String)file_info.get("file_id");	
				file_size= (String)file_info.get("fileSize");
				originFileName= (String)file_info.get("originFileName");
				fileInput = (String)file_info.get("fileInput");
				
				//System.out.println("originFileName====>"+originFileName);
				//System.out.println("fileInput====>"+fileInput);
				
				if (file_name != null) {  
			    	  if (fileInput.equals("attachFile")){
			    		  file_name_3=(String)file_info.get("fileName");
						  file_id_3=(String)file_info.get("file_id");	
						  file_size_3= (String)file_info.get("fileSize");
						  originFileName_3= (String)file_info.get("originFileName");
			    	  }
			    	  
			    	  if (fileInput.equals("attachFile_1")){
			    		  file_name_1=(String)file_info.get("fileName");
						  file_id_1=(String)file_info.get("file_id");	
						  file_size_1= (String)file_info.get("fileSize");
						  originFileName_1= (String)file_info.get("originFileName");
			    	  }
			    	  
			    	  if (fileInput.equals("attachFile_2")){
			    		  file_name_2=(String)file_info.get("fileName");
						  file_id_2=(String)file_info.get("file_id");	
						  file_size_2= (String)file_info.get("fileSize");
						  originFileName_2= (String)file_info.get("originFileName");
			    	  }
			    	 
				}

			}
			
			String opic_logo_nm = originFileName_3;
			String opic_logo_id = file_id_3;
			
			String ts_logo_nm = originFileName_1;
			String ts_logo_id = file_id_1;
			
			String admin_logo_nm = originFileName_2;
			String admin_logo_id = file_id_2;

			String ent_nm = CommonUtil.nvl(multi.getParameter("ent_nm"));	
			String ent_alias = CommonUtil.nvl(multi.getParameter("ent_alias"));	
			String addr = CommonUtil.nvl(multi.getParameter("addr"));	
			String start_dt = CommonUtil.nvl(multi.getParameter("start_dt"));	
			String close_dt = CommonUtil.nvl(multi.getParameter("close_dt"));	
			String use_yn = CommonUtil.nvl(multi.getParameter("use_yn"));	
			String service_gubun = CommonUtil.nvl(multi.getParameter("service_gubun"));
			String ref_url = CommonUtil.nvl(multi.getParameter("ref_url"));
			String ref_chk_yn = CommonUtil.nvl(multi.getParameter("ref_chk_yn"));
			String test_cost = CommonUtil.nvl(multi.getParameter("test_cost"));
			String test_cost_discount = CommonUtil.nvl(multi.getParameter("test_cost_discount"));
			
			String user_id = CommonUtil.nvl(multi.getParameter("user_id"));
			
			String retest_period = CommonUtil.nvl(multi.getParameter("retest_period"));
						
			con = (new CommonConnect()).getConnection_ds2();
			con.setAutoCommit(false);
			String[] param=null;
			String queryStr = QueryManager.getQuery("ent_insert.query");
			param= new String[19];
			param[0] = ent_nm;	
			param[1] = ent_alias;
			param[2] = addr;	
			param[3] = start_dt;
			param[4] = close_dt;
			param[5] = use_yn;
			param[6] = service_gubun;
			param[7] = ref_url;
			param[8] = ref_chk_yn;
			param[9] = test_cost;
			param[10] = test_cost_discount;
			param[11] = user_id;
			param[12] = retest_period;
			
			param[13] = opic_logo_nm;
			param[14] = opic_logo_id;
			param[15] = ts_logo_nm;
			param[16] = ts_logo_id;
			param[17] = admin_logo_nm;
			param[18] = admin_logo_id;
			
			list = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent",list);
			logger.debug("queryStr ==> "+"ent_insert.query");		     
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
